<template>
  <div>
    <!-- 弹出层的头部区域 -->
    <van-nav-bar title="频道管理">
      <template #right>
        <van-icon name="cross" size="0.37333334rem" color="white" @click="closePopup"/>
      </template>
    </van-nav-bar>
    <!-- 我的频道 -->
    <div class="my-channel-box">
      <div class="channel-title">
        <span>我的频道
          <span class="small-title">
            点击进入频道
          </span>
        </span>
        <span @click="isShowCross =!isShowCross">{{isShowCross?"完成":"编辑"}}</span>
      </div>
      <!-- 我的频道列表 -->
      <van-row type="flex">
        <van-col span="6" v-for="(item,index) in userChannelList" :key="item.id" @click="enterFn(item)">
          <div class="channel-item van-hairline--surround">
            {{item.name}}
            <!-- 删除的徽标 -->
            <van-badge color="transparent" class="cross-badge" v-if="isShowCross && item.id !== 0">
              <template #content>
                <van-icon
                  name="cross"
                  class="badge-icon"
                  color="#cfcfcf"
                  size="0.32rem"
                  @click="delFn(index)"
                />
              </template>
            </van-badge>
          </div>
        </van-col>
      </van-row>
    </div>

    <!-- 更多频道 -->
    <div class="more-channel-box">
      <div class="channel-title">
        <span>点击添加更多频道：</span>
      </div>
      <!-- 更多频道列表 -->
      <van-row type="flex">
        <van-col span="6" v-for="obj in unselectedChannelList" :key="obj.id">
          <div @click="addChannelFn(obj)" class="channel-item van-hairline--surround">{{obj.name}}</div>
        </van-col>
      </van-row>
    </div>
  </div>
</template>

<script>
import { getAllChannelListAPI } from '@/api/home'
import { Toast } from 'vant'
export default {
  name: 'ChannelManager',
  props: {
    // 父传子得到 用户已选择的用户列表
    userChannelList: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      isShowCross: false, // 是否显示频道右上角 ×徽标
      allChannelList: [] // 所isShowCross: false有频道列表
    }
  },
  computed: {
    // 根据所有的和已选择的计算出未选择的-用户没有选择的频道-要推荐的
    unselectedChannelList () {
      return this.allChannelList.filter(item => {
        // 判断每个频道对象item 是否在userChannelList中 不在才留下
        // filter 会留下返回会true的item对象 some方法-与every相对
        if (this.userChannelList.some(obj => obj.id === item.id)) {
          return false
        } else {
          return true
        }
      })
    }
  },
  async created () {
    const res = await getAllChannelListAPI()
    this.allChannelList = res.data.data.channels
    console.log(res)
  },
  methods: {
    // 关闭弹出层-子传父
    closePopup () {
      // 1.通知父亲-关闭弹出层
      this.$emit('close')
      // 2.重置编辑状态
      setTimeout(() => {
        this.isShowCross = false
      }, 20)
    },
    // 添加频道-子传父
    addChannelFn (item) {
      if (this.isShowCross === false) {
        Toast('请先点击编辑，再进行添加')
        return
      }
      this.$emit('add', item)
    },
    // 删除频道-子传父
    delFn (index) {
      this.$emit('del', index)
    },
    // 进入频道-子传父
    enterFn (item) {
      if (this.isShowCross === true) {
        Toast('编辑状态下不可进入频道')
        return
      }
      this.$emit('enter', item)
    }
  }
}
</script>

<style scoped lang="less">
.van-popup,
.popup-container {
  background-color: transparent;
  height: 100%;
  width: 100%;
}

.popup-container {
  display: flex;
  flex-direction: column;
}

.pop-header {
  height: 90px;
  background-color: #007bff;
  color: white;
  text-align: center;
  font-size: 14px;
  position: relative;
  .title {
    width: 100%;
    position: absolute;
    bottom: 15px;
  }
}

.pop-body {
  flex: 1;
  overflow: scroll;
  padding: 8px;
  background-color: white;
}

.my-channel-box,
.more-channel-box {
  .channel-title {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    line-height: 28px;
    padding: 0 6px;
  }
}

.channel-item {
  font-size: 12px;
  text-align: center;
  line-height: 36px;
  background-color: #fafafa;
  margin: 5px;
}

/*删除的微标 */
.cross-badge {
  position: absolute;
  right: -3px;
  top: 0;
  border: none;
}

/*提示文字 */
.small-title {
  font-size: 10px;
  color: gray;
}
</style>
